package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.article.IArticleClient;
import com.heima.behavior.service.ApBeHaviorService;
import com.heima.behavior.util.AppThreadLocalUtil;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.ArchiveUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ApBeHaviorServiceImpl implements ApBeHaviorService {

    @Autowired
    private CacheService cacheService;

    private static final int OPERATION_LIKE = 0;
    private static final String LIKE_BEHAVIOR = "like:";
    private static final String UNLIKE_BEHAVIOR = "unlike:";
    private static final String READ_BEHAVIOR = "read:";

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String redisKey = LIKE_BEHAVIOR + user.getId();
        if (dto.getOperation() == OPERATION_LIKE){
            Object exist = cacheService.hGet(redisKey, dto.getArticleId().toString());
            if (exist != null){
                return ResponseResult.okResult(AppHttpCodeEnum.AP_USER_BAHAVIOR_LIKE_EXIST);
            }
            cacheService.hPut(redisKey,dto.getArticleId().toString(), dto.getType().toString());
        }else {
            cacheService.hDelete(redisKey,dto.getArticleId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String redisKey = READ_BEHAVIOR + user.getId();
        dto.setCount(dto.getCount() + 1);
        cacheService.hPut(redisKey,dto.getArticleId().toString(), dto.getCount().toString());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String redisKey = UNLIKE_BEHAVIOR + user.getId();
        if (dto.getType() == OPERATION_LIKE){
            Object exist = cacheService.hGet(redisKey, dto.getArticleId().toString());
            if (exist != null){
                return ResponseResult.okResult(AppHttpCodeEnum.AP_USER_BAHAVIOR_LIKE_EXIST);
            }
            cacheService.hPut(redisKey,dto.getArticleId().toString(), dto.getType().toString());
        }else {
            cacheService.hDelete(redisKey,dto.getArticleId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
